
import 'package:flutter/material.dart';
import 'package:xim/common/style/colors.dart';
import 'package:xim/common/util/device.dart';
import 'package:xim/screen/profile_screen/profile_screen.dart';
import 'chat_book_screen/chat_book.dart';
import 'message_list_screen/chat_list_page.dart';

class MainScreen extends StatefulWidget {
  const MainScreen({Key? key}) : super(key: key);

  @override
  State<MainScreen> createState() => _MainScreenState();

}

class _MainScreenState extends State<MainScreen> with TickerProviderStateMixin {

  var selectedPage = 0;
  var pages = [ChatListScreen(), const ChatBookScreen(), ProfileScreen()];

  TabController? tabController;
  PageController? pageController;

  @override
  void initState() {
    super.initState();
    tabController = TabController(initialIndex: selectedPage, length: pages.length, vsync: this);
    pageController = PageController(initialPage: selectedPage);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: PageView(
          scrollDirection: Axis.horizontal,
          physics: const NeverScrollableScrollPhysics(),
          onPageChanged: (index)=>setState(() {
            selectedPage=index;
            tabController?.animateTo(index);
          }),
          controller: pageController,
          children: pages,
        ),
        bottomNavigationBar: BottomNavigationBar(
            currentIndex: selectedPage,
            onTap: (index){
              setState(() {
                selectedPage = index;
                pageController?.animateToPage(
                    index,
                    duration: const Duration(milliseconds: 300),
                    curve: Curves.ease
                );
              });
            },
            items: const [
              BottomNavigationBarItem(
                icon: Icon(Icons.message,),
                activeIcon: Icon(Icons.message,),
                label: "消息",
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.group),
                activeIcon: Icon(Icons.group,),
                label: "通讯录",
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.person),
                activeIcon: Icon(Icons.person,),
                label: "我的",
              ),
            ]
        )
    );
  }

}
